package com.abhimanyu.managedbeans;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.DateAxis;
import org.primefaces.model.chart.LineChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.abhimanyu.dataaccess.IDao;
import com.abhimanyu.dataaccess.model.DollarRate;

@Controller
@ManagedBean
@Scope(value="session")
public class DollarRateChartBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private LineChartModel dollarRatesChart;
	@Autowired
	//@ManagedProperty(value="inMemoryDollarRateDao") 	
	@Qualifier("googleDatastoreDollarRateDao")
	//@Qualifier("inMemoryDollarRateDao")	
	transient private IDao<DollarRate> dao;
	
	
	
	public DollarRateChartBean() {
	
	}

	@PostConstruct
	public void init() {
		System.out.println("PREPARING CHART");
		List<DollarRate> dollarRates = dao.fetchAll();
		System.out.println("DATA IS : " + dollarRates);
		dollarRatesChart = initLinearModel(dollarRates);
		dollarRatesChart.setTitle("Dollar Rate Daily Analysis Chart");
		dollarRatesChart.setZoom(true);
		dollarRatesChart.setAnimate(true);
		dollarRatesChart.getAxis(AxisType.Y).setLabel("Dollar Rate in Rupees");
		dollarRatesChart.setLegendPosition("e");
		DateAxis axis = new DateAxis("Dates");
		axis.setTickAngle(-50);
		// axis.setMax("2014-02-01");
		axis.setTickFormat("%b %#d, %y");
		dollarRatesChart.setExtender("ext");
		dollarRatesChart.getAxes().put(AxisType.X, axis);

	}

	DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

	private LineChartModel initLinearModel(List<DollarRate> dollarRates) {
		LineChartModel model = new LineChartModel();
		Map<String, LineChartSeries> chartSeriesMap = new HashMap<String, LineChartSeries>();
		LineChartSeries series = null;
		//series.setLabel("Series 1");

		for (DollarRate dollarRate : dollarRates) {
			/*if (dollarRate.getServiceName().contains("Xoom")) {
				
				series.set(df.format(dollarRate.getDate()),
						Double.parseDouble(dollarRate.getDollarRate()));
			} else {
				System.out.println("EXCLUDING NON-XOOM RATES : " + dollarRate.getServiceName());
			}*/
			series = chartSeriesMap.get(dollarRate.getServiceName());
			if(series == null) {
				series = new LineChartSeries();
				series.setLabel(dollarRate.getServiceName());
				chartSeriesMap.put(dollarRate.getServiceName(), series);
			}			
			series.set(df.format(dollarRate.getDate()),
					Double.parseDouble(dollarRate.getDollarRate()));			
		}

		for(ChartSeries chartSeries : chartSeriesMap.values()) {
			System.out.println(chartSeries.getData());
			model.addSeries(chartSeries);
		}
		return model;
	}

	public LineChartModel getDollarRatesChart() {
		return dollarRatesChart;
	}

	public void setDollarRatesChart(LineChartModel dollarRatesChart) {
		this.dollarRatesChart = dollarRatesChart;
	}
}
